import math
from typing import List


class Solution:
    def minimumSize(self, nums: List[int], maxOperations: int) -> int:
        left, right = 1, max(nums)

        while left < right:
            mid = (left + right) // 2

            need = 0
            for num in nums:
                if num > mid:
                    need += math.ceil(num / mid) - 1
                    if need > maxOperations:
                        break

            if need > maxOperations:  # 当前mid太小，无法实现
                left = mid + 1
            elif need < maxOperations:  # 当前mid可能过大，浪费的操作机会
                right = mid
            else:  # 当前mid正好用完了所有操作机会，但是可能当前的操作机会还能实现更小的
                right = mid

        return left


if __name__ == "__main__":
    print(Solution().minimumSize(nums=[9], maxOperations=2))  # 3
    print(Solution().minimumSize(nums=[2, 4, 8, 2], maxOperations=4))  # 2
    print(Solution().minimumSize(nums=[7, 17], maxOperations=2))  # 7
